﻿Public Class FIssueContract
    Public Shadows Sub ShowDialog(ByVal owner As IWin32Window)
        Me.Icon = My.Resources.harddisc_xp
        cboBuddy.DisplayMember = "Name"
        For Each clsBud As Entities.Buddy In Framework.Buddies.Manager.GetAll
            cboBuddy.Items.Add(New Buddy(clsBud.Id, clsBud.NameLocal))
        Next
        cboMultiplier.SelectedIndex = 2
        MyBase.ShowDialog(owner)
    End Sub
    Private Class Buddy
        Private mId As Integer
        Private mName As String
        Public ReadOnly Property Id() As Integer
            Get
                Return mId
            End Get
        End Property
        Public ReadOnly Property Name() As String
            Get
                Return mName
            End Get
        End Property
        Public Sub New(ByVal id As Integer, ByVal name As String)
            mId = id : mName = name
        End Sub
    End Class

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnIssue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIssue.Click
        Try
            Dim clsEP As New Framework.ErrorHandling.ErrorProviderEx(ErrorProvider1)

            If cboBuddy.SelectedItem Is Nothing Then clsEP.AddError(cboBuddy, "Select a buddy")
            Dim lSize As Long = Long.MaxValue
            lSize = CInt(NumericUpDown1.Value) * GetMultiplier()
            If Not IO.Directory.Exists(txtFolder.Text) Then
                clsEP.AddError(txtFolder, "Directory does not exist!")
            ElseIf Framework.Win32.GetFreeDiskSpace(txtFolder.Text) < lSize Then
                clsEP.AddError(txtFolder, "Not enough free disk space on volume!")
            End If
            If clsEP.Count = 0 Then
                Dim clsContract As New Entities.Backup.IssuedContract
                clsContract.Comment = txtComment.Text
                clsContract.StoragePath = txtFolder.Text
                clsContract.StorageSize = lSize
                clsContract.IssuedToBuddyId = CType(cboBuddy.SelectedItem, Buddy).Id
                EntityHandlers.Backup.IssuedContracts.Save(clsContract)
                Me.Close()
            End If
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub
    Private Function GetMultiplier() As Long
        Return CLng(1024 ^ (cboMultiplier.SelectedIndex + 1))
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop ' Environment.SpecialFolder.MyComputer

        Dim strCurrentPath As String = txtFolder.Text
        While Not IO.Directory.Exists(strCurrentPath) And strCurrentPath.Length > 0
            strCurrentPath = IO.Path.GetDirectoryName(strCurrentPath)
        End While
        FolderBrowserDialog1.SelectedPath = strCurrentPath
        FolderBrowserDialog1.ShowNewFolderButton = True
        FolderBrowserDialog1.Description = "Select folder to store backup in"

        Select Case FolderBrowserDialog1.ShowDialog(Me)
            Case Windows.Forms.DialogResult.OK
                txtFolder.Text = FolderBrowserDialog1.SelectedPath
        End Select
    End Sub
End Class